package edu.example;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.Vector;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;


public class AttachFileFrame extends JFrame
{
	private static final String dir = "files\\";

	//private ApplicationManagerCloud formatter;
	//File Choicer
	private JFileChooser fc;
	
	private TreeMap<String, Application> data = new TreeMap<String, Application>();
	
	private JScrollPane scrollPane;
	
	private JLabel lastNameHeader;
	private JLabel firstNameHeader;
	private JLabel midNameHeader;
	private JLabel emailHeader;
	private JLabel attachHeader;
	private JLabel filePathHeader;
	private JLabel removeHeader;
	
	private JPanel mainPanel;
	private JPanel buttonPanel;
	
	private JButton okButton;
	
	
	private BrowseFileHandler browseFileHandler;
	private RemoveFileHandler removeFileHandler;
	private OKHandler oKHandler;
	
	public AttachFileFrame(TreeMap<String, Application> intiData)
	{
		data = intiData;
		layoutGUI();
		this.setExtendedState(JFrame.MAXIMIZED_BOTH);
	}
	
	public void layoutGUI()
	{
		browseFileHandler = new BrowseFileHandler();
		removeFileHandler = new RemoveFileHandler();
		
		fc = new JFileChooser();
		
		mainPanel = new JPanel();
		scrollPane = new JScrollPane(mainPanel);
		
		GridBagLayout gbl = new GridBagLayout();
		mainPanel.setLayout(gbl);
		
		lastNameHeader = new JLabel("Last Name");
		firstNameHeader = new JLabel("First Name");
		midNameHeader = new JLabel("M.");
		emailHeader = new JLabel("E-mail");
		attachHeader = new JLabel("Attach");
		filePathHeader = new JLabel("Path");
		removeHeader = new JLabel("Remove");
		
		addComponent(lastNameHeader,			mainPanel,	0,	0,	1,	1);
		addComponent(midNameHeader,				mainPanel,	1,	0,	1,	1);
		addComponent(firstNameHeader,			mainPanel,	2,	0,	1,	1);
		addComponent(emailHeader,				mainPanel,	3,	0,	1,	1);
		addComponent(attachHeader,				mainPanel,	4,	0,	1,	1);
		addComponent(filePathHeader,			mainPanel,	5,	0,	1,	1);
		addComponent(removeHeader,				mainPanel,	6,	0,	1,	1);
		
		if(!data.isEmpty())
		{
			int counter = 0;

			Collection<Application> temp = data.values();
			Iterator<Application> it = temp.iterator();
				
			while(it.hasNext())
			{
				Application tempApp = it.next();
				JLabel lastNameLabel = new JLabel(tempApp.getApplicantLastName());
				JLabel firstNameLabel = new JLabel(tempApp.getApplicantFirstName());
				JLabel midtNameLabel = new JLabel(tempApp.getApplicantMiddleName());
				JLabel emailLabel = new JLabel(tempApp.getEmailAddress());
				JButton browseButton = new JButton("Attach File");
				JButton removeButton = new JButton("Remove");
				JTextField addressField = new JTextField(10);
				addressField.setEditable(false);
				
				addComponent(firstNameLabel,		mainPanel,	0,	counter+1,	1,	1);
				addComponent(midtNameLabel,			mainPanel,	1,	counter+1,	1,	1);
				addComponent(lastNameLabel,			mainPanel,	2,	counter+1,	1,	1);
				addComponent(emailLabel,			mainPanel,	3,	counter+1,	1,	1);
				addComponent(browseButton,			mainPanel,	4,	counter+1,	1,	1);
				addComponent(addressField,			mainPanel,	5,	counter+1,	1,	1);
				addComponent(removeButton,			mainPanel,	6,	counter+1,	1,	1);
				
				browseButton.addActionListener(browseFileHandler);
				removeButton.addActionListener(removeFileHandler);
				counter++;
			}
		}
		
		okButton = new JButton("OK");
		buttonPanel = new JPanel();		
		buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		buttonPanel.add(okButton);
		
		this.add(scrollPane, BorderLayout.CENTER);
		this.add(buttonPanel, BorderLayout.SOUTH);
		
		oKHandler = new OKHandler();
		okButton.addActionListener(oKHandler);
	}	
	
	public static void main(String[] args)
	{
		File file1 = new File("AY(Up).txt");
		File file2 = new File("AY-map.txt");
		File file3 = new File("OTS.txt");
		File file4 = new File("OTS-map.txt");
		ApplicationManager formatter = new ApplicationManager(null, file1, file2, file3, file4);
		AttachFileFrame frame = new AttachFileFrame(formatter.getAllApps());
		frame.setVisible(true);
	}
	
	/**
	 * GridBadLayout helper method
	 */
	public void addComponent(	JComponent jc,
			Container c,
			int x,
			int y,
			int w,
			int h)
	{
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = w;
		gbc.gridheight = h;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.insets = new Insets(5, 5, 5, 5);
		c.add(jc, gbc);
	}
	
	class BrowseFileHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent ae)
		{
			 int returnVal = fc.showOpenDialog(AttachFileFrame.this);
			 
			 if (returnVal == JFileChooser.APPROVE_OPTION) 
			 {
				 File file = fc.getSelectedFile();

				 //Find out which button was clicked
				 JButton button = (JButton) ae.getSource();
				 for(int i = 0; i< mainPanel.getComponentCount(); i++)
				 {
					 Component[] a = mainPanel.getComponents();
					 if(a[i] == button)
					 {
						 JTextField field = (JTextField) a[i+1];
						 field.setText(file.getPath());
					 }
				 }
			}
		}
	}
	
	class RemoveFileHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent ae)
		{
			JButton button = (JButton) ae.getSource();
			//Find out which button was clicked
			for(int i = 0; i< mainPanel.getComponentCount(); i++)
			{
				Component[] a = mainPanel.getComponents();
				if(a[i] == button)
				{
					JTextField field = (JTextField) a[i-1];
					field.setText(null);
				}
			}
		}
	}
	
	class OKHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent ae) 
		{
			for(int i = 0; i< mainPanel.getComponentCount(); i = i+7)
			{
				Component[] a = mainPanel.getComponents();
				if(i > 6)
				{
					String selectedFilePath = null;
					
					//Get application E-mail and selected file path
					JLabel temp1 = (JLabel) a[i+3];
					JTextField temp = (JTextField) a[i+5];
					String targetEmail = temp1.getText();
					selectedFilePath = temp.getText();
					
					//Get actual file
					File selectedFile = new File(selectedFilePath);
					
					if(!selectedFilePath.isEmpty())
					{
						//Create Directory  
						String targetDir = dir+targetEmail;
						File des = new File(targetDir);
						des.mkdir();
						
						//Create target file
						File targetFile = new File(targetDir+"\\"+selectedFile.getName());
						
						//Copy file
						try 
						{
							copyFile(selectedFile, targetFile);
							
							Application tempApp = data.get(targetEmail);
							tempApp.setRelatedFile(targetDir+"\\"+selectedFile.getName());

							
						}
						catch (IOException e) {}
					}
				}	
			}
			AttachFileFrame.this.setVisible(false);
		}
	}
	
	public void copyFile( File from, File to ) throws IOException 
	{
	    Files.copy( from.toPath(), to.toPath() );
	}
}
